#include <amxmodx>
#include <amxmisc>
#define MAX_players 32
#define MAX_menudata 1024

new ga_PlayerName[MAX_players][32]
new ga_PlayerAuthID[MAX_players][35]
new ga_PlayerID[MAX_players]
new ga_PlayerIP[MAX_players][16]
new ga_MenuData[MAX_menudata]
new ga_Choice[2]
new gi_VoteStarter
new gi_MenuPosition
new gi_Sellection
new gi_TotalPlayers
new gi_SysTimeOffset = 0
new i
new ga_AdminName[32]
new ga_UserName[32]
//pcvars
new gi_LastTime
new gi_DelayTime
new gf_Ratio
new gf_MinVoters
new gf_BF_Ratio
new gi_BanTime
new gi_Disable
new gi_BanType

public plugin_init()
{
	register_plugin("voteban menu","1.7","hjvl/Chuvi")
	register_clcmd("say /voteban","SayIt" )
	register_menucmd(register_menuid("ChoosePlayer"), 1023, "ChooseMenu")
	register_menucmd(register_menuid("VoteMenu"), 1023, "CountVotes")
	
	gi_LastTime=register_cvar("amx_voteban_lasttime","0")
	gi_DelayTime=register_cvar("amxx_voteban_delaytime","60")
	gf_Ratio=register_cvar("amxx_voteban_ratio","0.50")
	gf_MinVoters=register_cvar("amxx_voteban_minvoters","0.0")
	gf_BF_Ratio=register_cvar("amxx_voteban_bf_ratio","0.0")
	gi_BanTime=register_cvar("amxx_voteban_bantime","5")
	gi_Disable=register_cvar("amxx_voteban_disable","0")
	gi_BanType=register_cvar("amxx_voteban_type","0")
}

public SayIt(id)
{
	if(get_pcvar_num(gi_Disable))
	{
		client_print(id,print_chat,"[AMXX]amx_votaban disabled")
		return 0
	}
	
	new Elapsed=get_systime(gi_SysTimeOffset) - get_pcvar_num(gi_LastTime)
	new Delay=get_pcvar_num(gi_DelayTime)
	
	if( (Delay > Elapsed) && !is_user_admin(id) )
	{
		new seconds = Delay - Elapsed
		client_print(id,print_chat,"[AMXX] До нового голосования осталось %d секунд. Подождите, пожалуйста.", seconds)
		return 0
	}
	
	get_players( ga_PlayerID, gi_TotalPlayers )
	
	for(i=0; i<gi_TotalPlayers; i++)
	{
		new TempID = ga_PlayerID[i]
		if(is_user_admin(TempID)&&!is_user_hltv(TempID))
		{
			if(!is_user_admin(id))
			{
				get_user_name( TempID, ga_AdminName, 31 )
				get_user_name( id, ga_UserName, 31 )
				client_print(0,print_chat,"[Voteban]Уважаемый %s.На сервере есть следующие админы:",ga_UserName)
				show_admins(id)
				client_print(0,print_chat,"[Voteban]Voteban отключен.")
				
				return 0
			}
			if(is_user_admin(id))
			{
				get_user_name( TempID, ga_AdminName, 31 )
				client_print(0,print_chat,"%s! Ты-админ. Бань читера сам через amx_banmenu.",ga_AdminName)
				client_print(0,print_chat,"Не надо ни с кем совещаться. :)")
				return 0
			}
		}
		
		if(TempID == id)
			gi_VoteStarter=i
		
		get_user_name( TempID, ga_PlayerName[i], 31 )
		get_user_authid( TempID, ga_PlayerAuthID[i], 34 )
		get_user_ip( TempID, ga_PlayerIP[i], 15, 1 )
	}
	
	gi_MenuPosition = 0
	ShowPlayerMenu(id)
	return 0
}

public ShowPlayerMenu(id)
{
	new arrayloc = 0
	new keys = (1<<9)
	
	arrayloc = format(ga_MenuData,(MAX_menudata-1),"Меню бана игроков ^n")
	for(i=0; i<8; i++)
		if( gi_TotalPlayers>(gi_MenuPosition+i) )
	{
		arrayloc += format(ga_MenuData[arrayloc],(MAX_menudata-1-arrayloc),"%d. %s^n", i+1, ga_PlayerName[gi_MenuPosition+i])
		keys |= (1<<i)
	}
	if( gi_TotalPlayers>(gi_MenuPosition+8) )
	{
		arrayloc += format(ga_MenuData[arrayloc],(MAX_menudata-1-arrayloc),"^n9. Больше")
		keys |= (1<<8)
	}
	arrayloc += format(ga_MenuData[arrayloc],(MAX_menudata-1-arrayloc),"^n0. Выход")
	
	show_menu(id, keys, ga_MenuData, 20, "ChoosePlayer")
	return PLUGIN_HANDLED 
}

public ChooseMenu(id, key)
{
	switch(key)
	{
		case 8:
		{
			gi_MenuPosition=gi_MenuPosition+8
			ShowPlayerMenu(id)
		}
		case 9:
		{
			if(gi_MenuPosition>=8)
			{
				gi_MenuPosition=gi_MenuPosition-8
				ShowPlayerMenu(id)
			}
			else
				return 0
		}
		default:
	{
		gi_Sellection=gi_MenuPosition+key
		new Now=get_systime(gi_SysTimeOffset)
		set_pcvar_num(gi_LastTime, Now)
		
		run_vote()
		return 0
	}
}

return PLUGIN_HANDLED
}

public run_vote()
{
log_amx("Voteban запустил %s против %s %s", ga_PlayerName[gi_VoteStarter], ga_PlayerName[gi_Sellection], ga_PlayerAuthID[gi_Sellection])
format(ga_MenuData,(MAX_menudata-1),"Забанить суку %s на %d минут?^n1. Да! В баню гандона^n2. Нет! Пощадить нуба",ga_PlayerName[gi_Sellection], get_pcvar_num(gi_BanTime))
ga_Choice[0] = 0
ga_Choice[1] = 0
show_menu( 0, (1<<0)|(1<<1), ga_MenuData, 15, "VoteMenu" )
set_task(15.0,"outcom")
return 0
}

public CountVotes(id, key)
{
++ga_Choice[key]
return PLUGIN_HANDLED
}

public outcom()
{
new TotalVotes = ga_Choice[0] + ga_Choice[1]
new Float:result = (float(ga_Choice[0]) / float(TotalVotes))

if( get_pcvar_float(gf_MinVoters) >= ( float(TotalVotes) / float(gi_TotalPlayers) ) )
{
	client_print(0,print_chat,"[AMXX] Слишком мало людей хотят забанить %s!", ga_PlayerName[gi_Sellection])
	return 0
}
else
{
	if( result < get_pcvar_float(gf_BF_Ratio) )
	{
		client_print(0,print_chat,"[AMXX] The vote back fired at %s, he is banned for %d minutes", ga_PlayerName[gi_VoteStarter], get_pcvar_num(gi_BanTime))
		ActualBan(gi_VoteStarter,0)
		log_amx("[AMXX] The vote back fired at %s, he is banned for %d minutes", ga_PlayerName[gi_VoteStarter], get_pcvar_num(gi_BanTime))
	}
	
	if( result >= get_pcvar_float(gf_Ratio) )
	{
		client_print(0,print_chat,"[AMXX] Голосование завешено. %s отправлен в баню на %d минут", ga_PlayerName[gi_Sellection], get_pcvar_num(gi_BanTime))
		log_amx("[AMXX] The vote succeeded: %s is banned for %d minutes", ga_PlayerAuthID[gi_Sellection], get_pcvar_num(gi_BanTime))
		ActualBan(gi_Sellection,1)
	}
	else
	{
		client_print(0,print_chat,"[AMXX] Голосование завершено провалом. Никто не забанен.")
		log_amx("[AMXX] The voteban dit not sucseed.")
	}
}
client_print(0,print_chat,"Из %d игроков, %d проголосовали за бан.", gi_TotalPlayers, ga_Choice[0])

return 0
}

public ActualBan(Selected,Status)
{
new Type = get_pcvar_num(gi_BanType)
new reason[50]
if(Status==1)
{
	formatex(reason,charsmax(reason),"Voteban");
}
else
{
	formatex(reason,charsmax(reason),"Voteban_Fail");
}
switch(Type)
{
	case 1:
		server_cmd("addip %d %s", get_pcvar_num(gi_BanTime), ga_PlayerIP[Selected])
		case 2:
			server_cmd("amx_ban %d #%d %s", get_pcvar_num(gi_BanTime),get_user_userid(ga_PlayerID[Selected]),reason)
		default:
		server_cmd("banid %d %s kick", get_pcvar_num(gi_BanTime), ga_PlayerAuthID[Selected])
	}
	return 0 
}

public show_admins(id)
{
	new Players[32], Number_Of_Players,name[32],out[130]
	new total_admins=0
	get_players(Players, Number_Of_Players)		
	for (new Player=0; Player<Number_Of_Players; Player++)
	{
		if(is_user_connected(Players[Player])&&is_user_admin(Players[Player]))
		{
			total_admins++
			get_user_info(Players[Player],"name",name,sizeof(name))
			if(total_admins==1)
				add(out,sizeof(out),name)
			else
			{
				format(name,sizeof(name),", %s",name)
				add(out,sizeof(out),name)
			}
		}
	}
	if(total_admins==0)
	{
		add(out,sizeof(out),"No admins online.")
	}
	else
	{
		add(out,sizeof(out),".")
	}
	client_print(0,print_chat,"%s",out)
	
	return PLUGIN_CONTINUE
	
}
/* AMXX-Studio Notes - DO NOT MODIFY BELOW HERE
*{\\ rtf1\\ ansi\\ deff0{\\ fonttbl{\\ f0\\ fnil Tahoma;}}\n\\ viewkind4\\ uc1\\ pard\\ lang1049\\ f0\\ fs16 \n\\ par }
*/
